import Vue from 'vue'
import VueRouter from 'vue-router'
import Admin from '../views/Admin'
import Login from "../views/Login"
import NotFound from "../views/NotFound"
import NoPerssion from "../views/NoPerssion"

Vue.use(VueRouter)

const routes = [
  {
    path:'/',
   redirect:"/admin"
  },
  {
    path:"/login",
    name:"login",
    component:Login
  },
  {
    path:"/admin",
    component:Admin,
    admin:true,
    redirect:"/admin/dashBoard",
    children:[
      {
        path:"/admin/dashBoard",
        name:"仪表盘",
        icon:"el-icon-pie-chart",
        meta:{
          isNav:true,
          isOnlyNav:true,
          roles:["admin","superAdmin","edit"]
        },
        component:()=>import ("@/views/DashBoard")
      },
      {
        path:"/admin/settings",
        name:"设置",
        icon:"el-icon-setting",
        meta:{
          isNav:true,
          isOnlyNav:true,
          roles:["superAdmin","edit"]
        },
        component:()=>import ("@/views/Settings")
      },
      {
        path:"/admin/message",
        name:"消息中心",
        icon:"el-icon-message",
        meta:{
          isNav:true,
          isOnlyNav:true,
          roles:["admin","superAdmin","edit"]
        },
        component:()=>import ("@/views/Message")
      },
      {
        path:"/admin/noPerssion",
        name:"没有权限",
        icon:"el-icon-message",
        meta:{
          isNav:false,
          roles:["admin","superAdmin","edit"]
        },
        component:NoPerssion
      },
      {
        path:"/admin/article",
        redirect:"/admin/article/artlists",
        name:"文章管理",
        icon:"el-icon-notebook-2",
        component:()=>import("@/views/Articles"),
        meta:{
          isNav:true,
          isOnlyNav:false,
          roles:["superAdmin","edit","admin"]
        },
        children:[
          {
            path:"/admin/article/artlists",
            name:"文章列表",
            icon:"el-icon-notebook-2",
            meta:{
              roles:["superAdmin","edit","admin"]
            },
            component:()=>import("@/views/Articles/ArtLists"),
          },
          {
            path:"/admin/article/addArt",
            name:"文章添加",
            icon:"el-icon-edit",
            meta:{
              roles:["superAdmin","edit","admin"]
            },
            component:()=>import("@/views/Articles/AddArt")
          },
          {
            path:"/admin/article/editArt",
            name:"文章编辑",
            icon:"el-icon-edit-outline",
            meta:{
              roles:["superAdmin","edit"]
            },
            component:()=>import("@/views/Articles/EditArt")
          }
        ]
      }
    ]
  },
  {
    path:"/404",
    name:"notFound",
    component:NotFound
  }
]
const router = new VueRouter({
  routes
})
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

router.beforeEach((to,from,next)=>{
  //开始判断放行条件，去登录注册放行
  const accessToken=localStorage.getItem('accessToken')
  //取到用户信息
  const userInfo=JSON.parse(localStorage.getItem("userInfo"))
  if(to.fullPath==="/login"){
    next()
  }else{
    //获取token,存在就放行，不存在重定向到登录，并且携带参数
    if(accessToken){
      //根据角色权限，进行路由拦截
      let flag=to.meta.roles.some(role=>{
        return role===userInfo.role
      })
      if(flag){
        //有权限，放行
        next()
      }else{
        //没权限去未授权页面
        next("/admin/noPerssion")
      }
    }else{
      next({name:'login',params:{path:to.fullPath}})
    }
  }
})

export default router
